<?php
/**
 * 客户基本信息上传
 */
namespace app\adminweb\controller;
use think\Controller;
use \think\Validate;
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
class BuildingPoint extends Pre
{
    //添加当前点位信息
    public function inserts(){
        $BuildingPoint = new \app\common\model\BuildingPoint();
        return $BuildingPoint -> inserts();
    }
    //查询当前所有楼盘点位
    public function list(){
        $data = input('param.');
        if($data['key']){
            $data['key'] = trim($data['key']);
            $data['page'] = 1;
            $map[] = ['name|company','like',"%$data[key]%"];
            $building = \app\common\model\Building::field('id,name,company,address')
                ->page($data['page'],$data['limit'])
                -> where($map)-> order('id DESC') -> select();
                if($building)
                    $datawang = $building -> toArray();
        }else{
            $datain = array_unique(\app\common\model\Building::column('company'));
            foreach ($datain as $value){
                $company[] = $value;
            }
            foreach ($company as $val){
                $map[] = ['company','eq',$val];
                $building = \app\common\model\Building::field('id,name,company,address')
                    -> where($map)-> order('id DESC') -> select() -> toArray();
                $buildinginfo[] = $building;
                unset($map);
            }
            foreach ($buildinginfo as $ve){
                foreach ($ve as $v){
                    $datawang[] = $v;
                }
            }
            if($data['page'] > 1){
                $num = ($data['page']-1)*10;
                for($i=0;$i<10;$i++){
                    $xian[] = $datawang[$num+$i];
                }
                $datawang = array_filter($xian);
            }else{
                for($i=0;$i<10;$i++){
                    $xian[] = $datawang[$i];
                }
                $datawang = array_filter($xian);
            }
        }
        foreach($datawang as $value){
            $result = \app\common\model\BuildingPoint::
                    where([
                         'building_id'=> $value['id'] ,
                         'action' => 0,
                    ]) ->order('role ASC')-> select()  -> toArray();
            if($result){
                foreach ($result as $val){
                    $arr = explode(',',$val['point']);
                    $array = ['name'=> $arr[0],'sign'=> $arr[1]=='%'?0:1];
                    $tmp[] = $array;
                }
                $value['point'] = $tmp;
            }else{
                $value['point'][] = '';
            }
            //渠道
            $result1 = \app\common\model\BuildingPoint::
                    where([
                        'building_id'=> $value['id'] ,
                        'action' => 1,
                    ])-> find();
            if($result1){
                $result1 = $result1 -> toArray();
                $res = explode(',',$result1['point']);
                $result1['name'] = $res[0];
                $result1['sign'] = $res[1]=='%'?0:1;
            }
            $result2 = \app\common\model\BuildingPoint::
                    where([
                        'building_id'=> $value['id'] ,
                        'action' => 2,
                    ])-> find();
            if($result2){
                $result2 = $result2 -> toArray();
                $res2 = explode(',',$result2['point']);
                $result2['name'] =  $res2[0];
                $result2['sign'] = $res2[1]=='%'?0:1;
                $resultin = array($result1,$result2);
            }else{
                $resultin[] = '';
            }
            $value['one'] = $resultin;
            //点位
            $result3 = \app\common\model\BuildingPoint::
            where([
                'building_id'=> $value['id'] ,
                'action' => 3,
            ])-> find();
            if($result3){
                $result3 = $result3 -> toArray();
                $res4 = explode(',',$result3['point']);
                $result3['name'] = $res4[0];
                $result3['sign'] = $res4[1]=='%'?0:1;
            }
            $result4 = \app\common\model\BuildingPoint::
            where([
                'building_id'=> $value['id'] ,
                'action' => 4,
            ])-> find();
            if($result4){
                $result4 = $result4 -> toArray();
                $res6 = explode(',',$result4['point']);
                $result4['name'] = $res6[0];
                $result4['sign'] = $res6[1]=='%'?0:1;
                $resultin1 = array($result3,$result4);
            }else{
                $resultin1[] = '';
            }
            $value['two'] = $resultin1;
            //转介
            $result5 = \app\common\model\BuildingPoint::
            where([
                'building_id'=> $value['id'] ,
                'action' => 5,
            ])-> find();
            if($result5){
                $result5 = $result5 -> toArray();
                $res8 = explode(',',$result5['point']);
                $result5['name'] = $res8[0];
                $result5['sign'] = $res8[1]=='%'?0:1;
                $resultin2 = array($result5);
            }else{
                $resultin2[] = '';
            }
            $value['three'] = $resultin2;
            $building1[] = $value;
            unset($tmp);
            unset($resultin);
            unset($resultin1);
            unset($resultin2);
        }
        $build = \app\common\model\Building::where($map) -> count();
        return json(['code' => 0,'msg'=> '','count'=> $build,'data'=>$building1]);
    }
}